// 导入路由
import router from '@/router'
import { storeA } from '@/piniaStore/storeA'
import axios from 'axios'
import { ElMessage, ElNotification } from 'element-plus'
let piniaStoreA = storeA()

interface data{
    username: string,
    password: string,
    phone?:number
    address?:string
}
var data:data={
    username:"",
    password:""
}
var Data = {
    username:"",
    password:"",
     address: "",
    shopnumber: ""
}
var _Data = {
    username:"",
    password:"",
}

var config = {
    method: 'post',
    url: 'http://wm.tocode.store',
    headers: {
        'Content-Type': 'application/json'
    },
    data: ""
};



// export interface User{
//     Username:string,
//     Password:string,
//     Type:string,
//     Phone?:number
//     Address?:string
// }
function Login(User:user,isUser:boolean){
    config.url="http://wm.tocode.store"
    //修改发送数据
    _Data.username = User.Username
    _Data.password = User.Password
    if(!isUser){
        config.url+="/loginHost"
    }
    else{
        config.url+="/loginUser"
    }
    config.data = JSON.stringify(_Data)
    // config.url="/api/loginUser"
    //假装成功登录//////////////////////////////
        //  //储存到本地信息
        // localStorage.setItem("User", JSON.stringify(User));
        // //修改全局状态
        // piniaStoreA.setisNoLogin(false)
        // // 执行路由跳转
        // router.push({
        //     path:'/',
        //     name: 'home',
        // })
        ///////////////////////////////////////
    axios(config)
    .then(function (response:any) {
        if(response.data.status==-1){
            console.log(response.data)
            ElMessage({
                message: '没有这个用户！你一定是记错了！',
                type: 'error',
              })
        }
        else{
           console.log(response);
            //储存到本地信息
           localStorage.setItem("Usertoken", JSON.stringify(response.data.token));
           //修改全局状态
           piniaStoreA.token = response.data.token
           piniaStoreA.setisNoLogin(false)
           // piniaStoreA.user = response
           // 执行路由跳转
           router.push({
               path:'/',
               name: 'home',
           })
           ElNotification({
               title: '登录成功',
               message: '欢迎光临！',
               type: 'success',
             })
        }
       
    })
    .catch(function (error:any) {
        console.log(error);
        if(error.message==="Request failed with status code 404"){
            ElMessage({
                message: '服务器无响应！请稍后再试',
                type: 'warning',
              })
        }
        else{
            ElMessage({
                message: '出现错误！',
                type: 'error',
              })
        }
    });
   
}
function Register(User:user){
    Data.username = User.Username
    Data.password = User.Password
    Data.address = User.Address
    config.url="http://wm.tocode.store"
    if(User.Type==="商家"&&User.shopName!=undefined){
        console.log("商家注册")
        Data.shopnumber=User.shopName
        config.url+="/regHost"
    }
    else{
        console.log("用户注册")
        Data.shopnumber=""
        config.url+="/regUser"
    }
    config.data = JSON.stringify(Data)
    axios(config)
    .then(function (response:any) {
        if(response.data.status==1){
            console.log(response.data.token);
            //储存到本地信息
           localStorage.setItem("Usertoken", JSON.stringify(response.data.token));
           //修改全局状态
           piniaStoreA.setisNoLogin(false)
           piniaStoreA.user = User
           piniaStoreA.token = response.data.token
           // 执行路由跳转
           router.push({
               path:'/',
               name: 'home',
           })
           ElMessage({
               message: '注册成功！',
               type: 'success',
             })
        }
        else{
            ElMessage({
                message: '用户名已存在，请重新注册',
                type: 'error',
              })
        }
       
    })
    .catch(function (error:any) {
        console.log(error);
        if(error.message==="Request failed with status code 404"){
            ElMessage({
                message: '服务器无响应！请稍后再试',
                type: 'warning',
              })
        }
        else{
            // 测试代码，务必记得删除
            // localStorage.setItem("User", JSON.stringify(User));
            // //修改全局状态
            // piniaStoreA.setisNoLogin(false)
            // piniaStoreA.user = User
            // // 执行路由跳转
            // router.push({
            //     path:'/',
            //     name: 'home',
            // })
            ElMessage({
                message: '出现错误！这肯定是后端的锅！',
                type: 'error',
              })
        }
    });
}
export{
    Login,Register
}

